package extractor;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import preprocess.SelectPvSearchClickByBuyerSamples;
import trouger.dmt.JoinTableRunner;
import trouger.dmt.JoinType;
import utils.Utils;


public class MergeBuyerFeature implements Tool{
	public static String getPvSearchClickWithBuyerPath(String date) {
		return Utils.rootDir + date + "/PvSearchClickWithBuyer/";
	}
	@Override 
	public Configuration getConf() {
		return null;
	}
	
	@Override
	public void setConf(Configuration conf) {
		
	}
	/*
	 * (non-Javadoc)
	 * UserFiltered Table
	 * Path: /group/tbsc-dev/xzwang/clickprediction/$date/PvSearchClickWithBuyer/
	 * 1. search[1~25]
	 * 26.UUID
	 * 27.pv时间
	 * 28.直达后类目
	 * 29.商品idlist
	 * 30. user[1~29]
	 * 59.clicklist[点击时间^A搜索的来源pid^A点击位置^A点击商品id(数字)^A是否为豆腐块商品^B....]
	 * */	 
	@Override
	public int run(String[] args) throws Exception {
		String output = getPvSearchClickWithBuyerPath(args[0]);
		String pvSearchClickDetail = SelectPvSearchClickByBuyerSamples.getSelectPvSearchClickByBuyerSamplesedPath(args[0]);
		String userExtendedProfile = UserExtendedExtractor.getUserExtendedProfilePath(args[0]);
		System.out.println("start job MergeBuyerFeature...");
		System.out.println("userExtendedProfile: " + userExtendedProfile);
		System.out.println("pvSearchClickDetail: " + pvSearchClickDetail);
		System.out.println("output: " + output);
		if (Utils.exitSystem(args))
			return -1;
		JoinTableRunner r = new JoinTableRunner();		
		r.addSourceTable(userExtendedProfile, "UTF-8", "\t");
		r.addSourceTable(pvSearchClickDetail, "UTF-8", "\t");
		r.addKeyGroup("1.3, 2.1");	
		r.setJoinType(JoinType.InnerJoin);
		StringBuilder columns = new StringBuilder();
		for (int i = 1; i <= 29; ++i)
			columns.append("2." + String.valueOf(i) + ", ");
		
		for (int i = 1; i <= 29; ++i) {
			columns.append("1." + String.valueOf(i) + ", ");
		}
		columns.append("2.30");
		String columnStr = columns.toString();
		System.out.println(columnStr);
		r.setResultFieldsEx(columnStr);
		r.setResultTable(output);
		r.run("MergeBuyerFeature");
		return 0;		 	
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Utils.checkArgs(args);
		
		// TODO Auto-generated method stub
		int res = ToolRunner.run(new MergeBuyerFeature(), args);
		System.exit(res);
	}

}

